<?php
declare (strict_types = 1);

namespace app\index\controller;

// use think\Request;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Config;
use app\middleware\Auth;

class Exhibits{
    
    protected $middleware = [Auth::class];
    //
    public function tab(){
        if(Request::isPost()){
            $tab=[
                ['name'=>'精选','type'=>'featured'],
                ['name'=>'全部','type'=>'all'],
            ];
            return json(['code'=>200,'success'=>true,'message'=>'获取成功','result'=>['tab'=>$tab,'current'=>0]]);
        }
    }
    //获取列表
    public function getList(){
        if(Request::isPost()){
            $zone_class=input('param.zone_class');
            $zone=input('param.zone');
            $keyword=input('param.keyword');
            $page=input('param.page',1);
            $size=input('param.size',10);
            $type=input('param.type','all');
            $where=[];
            if($zone_class){
                $where[]=['zone_class','=',$zone_class];
            }
            if($zone){
                $where[]=['zone','=',$zone];
            }
            if($keyword){
                $where[]=['customize_code|title','like',"%$keyword%"];
            }
            if($type=='featured'){
                $where[]=['is_featured','=',1];
            }
            
            $where[]=['is_hide','=',0];
            $data=Db::table('exhibits')->field('id as exhibits_id,zone_class,zone,customize_code,title,era,category,type,tex,style_mark,size,check_in_at,pieces,is_featured')
            ->field("CONCAT('https://darong.storage.1415926.com.cn',JSON_UNQUOTE(JSON_EXTRACT(cover, '$[0]'))) as cover")
            ->field('CONCAT("¥ ",price_desc) as price_desc,if(price_desc!="",1,0) as is_show_price')
            ->where($where)
            ->paginate([
                    'list_rows' => $size,
                    'page'      => $page,
                ]);
    
            $total=$data->total();
            $data=$data->items();
      
            $page_count=ceil($total/$size);
            $result=['code'=>200,'success'=>true,'message'=>'获取成功','result'=>['data'=>$data,'total'=>$total,'page'=>['current'=>$page,'size'=>$size,'total'=>$page_count]],'where'=>$where];
            return json($result);
        }
    }

    //通过Id获取展品信息
        public function getExhibitById(){
        if(Request::isPost()){
            // $id=input('param.id',0);
            $id=input('post.id/s',0);
            $openId=input('param.openId','');
            if($id==0){
                return json(['code'=>200,'success'=>false,'message'=>'参数出错']);
            }
            \think\facade\Log::info("/index/Exhibits/getExhibitById 请求：".json_encode(input('param.'),JSON_UNESCAPED_UNICODE));
            $redis=\think\facade\Cache::store('redis')->get('EXHIBIT_'.$id);
            $data=json_decode(json_encode($redis),true);
            if(!$data){
                // return json(['code'=>200,'success'=>true,'message'=>'获取成功','result'=>$redis,'t'=>time()]);
                $data=Db::table('exhibits')->field('id,zone_class,zone,customize_code,cover,title,era,category,type,tex,style_mark,size,angle,check_in_at,remark,pieces')
                ->field('CONCAT("¥ ",price_desc) as price_desc,if(price_desc!="",1,0) as is_show_price')
                ->where(['id'=>$id])->find();
                if($data['cover']){
                    $data['cover']=json_decode($data['cover'],true);
                    foreach($data['cover'] as $k=>$v){
                        $data['cover'][$k]='https://darong.storage.1415926.com.cn'.$v;
                    }
                }else{
                    $data['cover']=[];
                }
                \think\facade\Cache::store('redis')->set('EXHIBIT_'.$id,$data,600);
            }
            // $data['logo']['url']='https://darong.storage.1415926.com.cn/logo.jpg';
            // $data['logo']['title']='大荣木业';
            $data['logo']=getLogo();
            $data['account']['isCollect']=false;
            if($openId){
                $isCollect=Db::table('account_collection')->where(['exhibit_id'=>$id,'open_id'=>$openId,'is_cannel'=>0])->count();
                $data['account']['isCollect']=$isCollect?true:false;
            }
            return json(['code'=>200,'success'=>true,'message'=>'获取成功','result'=>$data]);
        }
    }
    
    //用户收藏
    public function setAccountCollection(){
        if(Request::isPost()){
            try{
                $openId=input('param.open_id','');
                $exhibit_id=input('param.exhibits_id',0);
                if($openId==''){
                    return json(['code'=>200,'success'=>false,'message'=>'参数出错']);
                }
                $account_collection=Db::table('account_collection')->field('is_cannel')->where(['open_id'=>$openId,'exhibit_id'=>$exhibit_id])->find();
                if(is_array($account_collection)){
                    Db::table('account_collection')->where(['open_id'=>$openId,'exhibit_id'=>$exhibit_id])->update([
                        'is_cannel'=>$account_collection['is_cannel']?0:1,
                        'update_time'=>time()
                    ]);
                }else{
                    Db::table('account_collection')->insert([
                        'account_id'=>input('param.account_id',0),
                        'open_id'=>$openId,
                        'exhibit_id'=>$exhibit_id,
                        'exhibition_id'=>input('param.exhibition_id',0),
                        'create_time'=>time()
                    ]);
                    $account_collection['is_cannel']=0;
                }
                
                $message=$account_collection['is_cannel']==0?'收藏成功':'取消收藏';
                return json(['code'=>200,'success'=>true,'message'=>$message,'result'=>$account_collection]);
            }catch (Exception $e) {
                return json(['code'=>200,'success'=>false,'message'=>$e]);
            }
        }
    }

    //通过code获取展品编号
    public function getExhibitIdByCode(){
        if(Request::isPost()){
            $code=input('param.code','');
            if($code==""){
                return json(['code'=>200,'success'=>false,'message'=>'参数出错']);
            }
            $id=Db::table('exhibits')->where(['customize_code'=>$code])->value('id');
            if($id){
                return json(['code'=>200,'success'=>true,'message'=>'获取成功','result'=>['exhibitId'=>$id]]);
            }
            return json(['code'=>200,'success'=>false,'message'=>'展品编号出错']);
        }
    }
}